/*
 * Created by IntelliJ IDEA.
 * User: amrk
 * Date: 28/04/2005
 * Time: 22:06:20
 */
package com.talios.jira.idea.actions;

import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.DataConstants;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.popup.JBPopupFactory;
import com.intellij.openapi.ui.popup.ListPopup;
import com.intellij.openapi.ui.popup.ListPopupStep;
import com.intellij.openapi.ui.popup.PopupStep;
import com.intellij.openapi.ui.popup.util.BaseListPopupStep;
import com.talios.jira.JiraException;
import com.talios.jira.browser.signals.JiraServerDetails;
import com.talios.jira.idea.JiraConfigurationComponent;
import com.talios.jira.rpc.JiraRpcClient;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class FixedInIdeaBuildAction extends AnAction {
    private static final Logger LOGGER = Logger.getInstance(FixedInIdeaBuildAction.class.toString());

    private class JiraVersion {
        private String id;
        private String name;

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String toString() {
            return name;
        }

        public JiraVersion(String id, String name) {
            this.id = id;
            this.name = name;
        }
    }

    @Override
    public void update(AnActionEvent event) {
        final Project project = (Project) event.getDataContext().getData(DataConstants.PROJECT);
        event.getPresentation().setEnabled(project != null);
    }

    public void actionPerformed(AnActionEvent event) {
        final Project project = (Project) event.getDataContext().getData(DataConstants.PROJECT);
        if (project == null) return;

        final JiraConfigurationComponent config = project.getComponent(JiraConfigurationComponent.class);

        JiraServerDetails jbServer = new JiraServerDetails(
                "http://jetbrains.net/jira",
                "jirabrowser",
                "G74Om4",
                false,
                false,
                "");

        JiraRpcClient rpc = new JiraRpcClient(jbServer);

        try {
            if (rpc.login()) {
                LOGGER.info("Logged into JetBrains JIRA Install, getting versions");
                final List<Map> versions = rpc.getVersions("IDEADEV");
                LOGGER.info(versions.size() + " versions found in JetBrains JIRA Install");

                List<JiraVersion> jiraVersions = new ArrayList<JiraVersion>();
                for (Map version : versions) {
                    jiraVersions.add(new JiraVersion((String) version.get("id"), (String) version.get("name")));
                }

                ListPopupStep popupStep = new BaseListPopupStep<JiraVersion>("Select IntelliJ Version", jiraVersions) {
                    public PopupStep onChosen(JiraVersion jiraVersion, boolean b) {
                        System.out.println("Selected version is " + jiraVersion);
                        config.openSearch(new FixedInIdeaBuildFeedBuilder(jiraVersion.getId(), jiraVersion.getName()), true);
                        return null;
                    }
                };

                ListPopup listPopup = JBPopupFactory.getInstance().createListPopup(popupStep);

                listPopup.showCenteredInCurrentWindow(project);

            }
        } catch (JiraException e) {
            LOGGER.error("Unable to lookup versions in JetBrains JIRA Install: " + e.getMessage(), e);
        }


    }

}